package com.yidu.hbats.service.impl.emp;

import com.github.pagehelper.PageHelper;
import com.yidu.hbats.dao.AtsEmpExpertMapper;
import com.yidu.hbats.model.AtsEmpExpert;
import com.yidu.hbats.service.emp.AtsEmpExpertService;
import com.yidu.springbootdemo.permission.domain.Tuser;
import com.yidu.springbootdemo.permission.service.TuserService;
import com.yidu.springbootdemo.permission.tools.Tools;
import com.yidu.utils.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @program: hbats
 * @description: 专家信息接口实现类
 * @author: 张超
 * @create: 2019-11-01 10:24
 */
@Service
public class AtsEmpExpertServiceImpl implements AtsEmpExpertService {

    @Resource
    AtsEmpExpertMapper atsEmpExpertMapper;

    @Override
    public int deleteAll(String ids) {
        if(StringUtils.isEmpty(ids)){
            return 0;
        }else{
            String[] idss = ids.split(",");
            for(int i = 0;i<idss.length;i++){
                AtsEmpExpert atsEmpExpert = new AtsEmpExpert();
                atsEmpExpert.setExpertId(Integer.valueOf(idss[i]));
                atsEmpExpert.setIsva(2);
                saveOrUpdate(atsEmpExpert);
            }
            return 1;
        }
    }

    @Override
    public List<AtsEmpExpert> findAllFour() {
        return atsEmpExpertMapper.findAllFour();
    }

    @Override
    public AtsEmpExpert selectByPrimaryKey(Integer expertId) {
        return atsEmpExpertMapper.selectByPrimaryKey(expertId);
    }

    @Resource
    TuserService tuserService;

    @Override
    public int saveOrUpdate(AtsEmpExpert atsEmpExpert) {
        if(StringUtils.isEmpty(atsEmpExpert.getExpertId())){
            atsEmpExpertMapper.insertSelective(atsEmpExpert);

            Tuser tuser = new Tuser();
            String rd = UUID.randomUUID().toString().replaceAll("-", "");
            tuser.setUserId(rd);
            tuser.setFilId(atsEmpExpert.getExpertId().toString());//对应对象id
            tuser.setCompanyNum("3");//类型字段 对应 1 , 领导  2,教练员 3,专家 4,运动员
            tuser.setCompanyName(atsEmpExpert.getExpertName());//对象名称
            tuser.setCompany("专家");//对象类型名称
            tuser.setUserName(atsEmpExpert.getExpertName());
            tuser.setUserPassword("123456");
            tuser.setUserState("Y");
            tuser.setUserFoundDate(Tools.time());
            tuser.setUserFoundUser(atsEmpExpert.getCreater());


            tuserService.add(tuser);
        }else{
            atsEmpExpertMapper.updateByPrimaryKeySelective(atsEmpExpert);

            Tuser tuser = new Tuser();
            tuser.setFilId(atsEmpExpert.getExpertId().toString());
            tuser.setCompanyNum("3");

            Tuser tuser1 = tuserService.findBycompanyNum(tuser);
            if(tuser1!=null){
                //如果是删除的话 就把用户表中的也删除
                if(atsEmpExpert.getIsva()!=null && atsEmpExpert.getIsva()==2){
                    if(tuser1!=null) {
                        tuserService.delete(tuser1.getUserId());
                    }
                }else{
                    tuser1.setCompanyName(atsEmpExpert.getExpertName());//对象名称
                    tuser1.setUserName(atsEmpExpert.getExpertName());
                    tuserService.update(tuser1);
                }
            }
        }

        return 0;
    }

    @Override
    public AtsEmpExpert findById(Integer id) {
        return atsEmpExpertMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<AtsEmpExpert> selectAll(AtsEmpExpert atsEmpExpert, Page page) {
        PageHelper.offsetPage(page.getStart(), page.getRows());

        List<AtsEmpExpert> list = atsEmpExpertMapper.selectAll(atsEmpExpert);

        PageHelper.startPage(page.getStart(), page.getRows());
        return list;
    }

    @Override
    public int selectCount(AtsEmpExpert atsEmpExpert) {
        return atsEmpExpertMapper.selectCount(atsEmpExpert);
    }

    @Override
    public List<AtsEmpExpert> findAll(AtsEmpExpert atsEmpExpert) {
        return atsEmpExpertMapper.findAll(atsEmpExpert);
    }
}